package com.zslh.web;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zslh.entity.Employee;
import com.zslh.entity.Message;
import com.zslh.service.MessageService;
import com.zslh.util.DateUtil;

public class SendMsgServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String sendMsg=request.getParameter("sendMsg");
		HttpSession session=request.getSession();
		Employee employee=(Employee)session.getAttribute("employee");
		String username=employee.getEmpName();
		
		Message msg=new Message();
		msg.setMessage(sendMsg);
		msg.setUsername(username);
		msg.setNowTime(DateUtil.getNow());
		MessageService service=new MessageService();
		service.save(msg);
		
		
		ServletContext sc=session.getServletContext();
		List<Message> msgList=(List<Message>) sc.getAttribute("msgList");
		
		if(msgList==null){
			msgList=new ArrayList<Message>();
		}
		
		msgList.add(msg);
		sc.setAttribute("msgList", msgList);
		response.sendRedirect("project.jspx");
	}
	
	

}
